package com.thundersoft.beautifulmusic.ui;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ActivityInfo;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.util.Log;
import android.view.KeyEvent;
import android.widget.ImageView;
import android.widget.SeekBar;
import android.widget.TextView;

import com.thundersoft.beautifulmusic.R;
import com.thundersoft.beautifulmusic.entity.Song;
import com.thundersoft.beautifulmusic.other.SongApplication;
import com.thundersoft.beautifulmusic.other.SongService;
import com.thundersoft.beautifulmusic.util.Constants;
import com.thundersoft.beautifulmusic.util.LogUtil;
import com.thundersoft.beautifulmusic.util.SongUtil;

public class SongDetailsActivity extends AppCompatActivity {
    private static final String TAG = "SongDetailsActivity";
    private ImageView mCover;
    private TextView mName;
    private SeekBar mSeekBar;
    private ImageView mType;
    private ImageView mLast;
    private ImageView mPlay;
    private ImageView mNext;
    private boolean mFlag = false;

    private final Handler handler = new Handler(Looper.getMainLooper()) {
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1:
                    mSeekBar.setProgress(msg.arg1);
                    break;
                case 2:
                    drawInterface((Song) msg.obj);
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_song_details);

        // 初始化各种组件
        init();
        Song mSong = (Song) getIntent().getSerializableExtra("song");

        // 初始化界面
        drawInterface(mSong);

        mType.setOnClickListener(v -> {

        });

        mPlay.setOnClickListener(v -> {
            Intent intent = new Intent("play");
            intent.putExtra("song", mSong);
            startService(intent);
        });

        // 点击上一首
        mLast.setOnClickListener(v -> {
            Intent intent = new Intent("play");
            intent.putExtra("song", mSong);
            startService(intent);
        });
        // 点击下一首
        mNext.setOnClickListener(v -> {
            Intent intent = new Intent("play");
            intent.putExtra("song", mSong);
            startService(intent);
        });

        // 进度条事件
        mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            /**
             * 拖动的中间过程
             * @param seekBar 当前组件
             * @param progress 拖动到哪个进度
             * @param fromUser ？？
             */
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                // Do nothing
            }

            /**
             * 拖动的第一下
             */
            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {
                // Do nothing
            }

            /**
             * 拖动结束
             */
            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onStop() {
        super.onStop();
        // close SeekBarThread
        mFlag = true;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if(keyCode == KeyEvent.KEYCODE_BACK){
            Intent intent = new Intent(this, MainActivity.class);
            startActivity(intent);
            return true;
        }
        return super.onKeyDown(keyCode, event);
    }

    /**
     * 初始化一些组件
     */
    private void init() {
        mCover = findViewById(R.id.details_song_cover);
        mName = findViewById(R.id.details_song_name);
        mSeekBar = findViewById(R.id.details_song_bar);
        mType = findViewById(R.id.details_play_type);
        mLast = findViewById(R.id.details_last);
        mPlay = findViewById(R.id.details_play);
        mNext = findViewById(R.id.details_next);
    }

    /**
     * 根据歌曲设置当前界面
     *
     * @param song 被选中的歌曲
     */
    private void drawInterface(Song song) {
        Bitmap songBitmap = SongUtil.optimizeSongBitmap(song.getAlbumId());
        mCover.setImageBitmap(songBitmap);
        mName.setText(song.getName());
    }
}